<?php

require_once "Validator.php";

/**
 * class Validator used for validating purposes
 */
class FormValidator {
	
	private $_errors = array();
	private $_formData = array();

	public function __construct($formData = null)
	{
		if(isset($formData) && is_array($formData))
			$this->_formData = $formData;
	}
	
	/**
	 * Function that checks if the formValidator has any errors
	 */
	public function hasError()
	{
		return (count($this->_errors) > 0);
	}
	
	/**
	 * Function that returns the errors
	 */
	public function getErrors()
	{
		return ($this->_errors);
	}
	
	/**
	 * Function that resets the errors
	 */
	public function resetErrors()
	{
		$this->_errors = array(); 
	}
	
	/**
	 * Function that sets new set of formData
	 */
	public function setFormData($formData)
	{
		if(isset($formData) && is_array($formData)){
			$this->resetErrors();
			$this->_formData = $formData;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid eMail address, FALSE otherwise
	 */
	public function isEmailAddress($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isEmailAddress($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	
	/**
	 * @return boolean TRUE if $var is a valid Personal ID Number, FALSE otherwise
	 */
	public function isPersonalIdNumber($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isPersonalIdNumber($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}

	/**
	 * @return boolean TRUE if $var is a valid Short Date address, FALSE otherwise
	 */
	public function isShortDate($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isShortDate($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Number, FALSE otherwise
	 */
	public function isNumeric($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isNumeric($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Floating Number, FALSE otherwise
	 */
	public function isFloat($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isFloat($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Integer Number, FALSE otherwise
	 */
	public function isInteger($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isInteger($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is in given range, FALSE otherwise
	 */
	public function isInRange($field, $min, $max, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isInRange($this->_formData[$field], $min, $max)){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid String, FALSE otherwise
	 */
	public function isString($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isString($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * @return boolean TRUE if $var is a valid Password, FALSE otherwise
	 */
	public function isPassword($field, $errMsg)
	{
		if(isset($this->_formData[$field]) && Validator::isPassword($this->_formData[$field])){
			return true;
		}else {
			$this->addError($field, $errMsg);
			return false;
		}
	}
	
	/**
	 * Helper function that adds the errors into the error array
	 */
	private function addError($field, $msg)
	{
		$this->_errors[] = array('field' => $field, 
								'value' => isset($this->_formData[$field]) ? $this->_formData[$field] : '', 
								'error' => $msg);
	}
}
